var api = require('../../utils/api.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        set2: 0,
        set: 0,
        id: '',
        tops: 32,
        show: 1,
        data1: [],
        data2: [],
        data3: [],
        data4: [],
        images: [],
        images1: [],
        images2: [],
        images3: [],
        images4: [],
        images5: [],
        images6: [],
        images7: [],
        allphoto: '',
        allnum: '',
        allvideo: '',
        mian: '',
        minor: '',
        allphoto1: '',
        allnum1: '',
        allvideo1: '',
        mian1: '',
        minor1: '',
        infos: []
    },
    setshow1() {
        this.setData({
            show: 1
        })
    },
    setshow2() {
        this.setData({
            show: 2
        })
    },

    onShow() {
        this.getinfobyid();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        that.setData({
            id: options.id
        })


        var infos = wx.getSystemInfoSync();
        console.log(infos);
        console.log(infos.statusBarHeight);
        this.setData({
            tops: infos.statusBarHeight + 4
        })

  
       
    },

    getinfobyid() {
        var that = this;

        var id = this.data.id;

        api.post({
            url: 'live/getinfobyid',
            data: {
                id: id
            },
            success: data => {
                console.log(data);

                if (data.code == 1) {

                    that.setData({
                        data1: data.data.data1,
                        data2: data.data.data2,
                        data3: data.data.data3,
                        data4: data.data.data4,
                        images: data.data.images,
                        images1: data.data.images1,
                        images2: data.data.images2,
                        images3: data.data.images3,
                        images4: data.data.images4,
                        images5: data.data.images5,
                        images6: data.data.images6,
                        images7: data.data.images7,
                        allnum: data.data.allnum,
                        allphoto: data.data.allphoto,
                        allvideo: data.data.allvideo,
                        mian: data.data.mian,
                        minor: data.data.minor,
                        infos: data.data
                    })
                }
            }
        })

    },

    //提交
    submit(e) {

        var info = {};
        var that = this;

        var data1 = this.data.data1;
        const jsonString = JSON.stringify(data1);
        info.data1 = jsonString;

        var data2 = this.data.data2;
        const jsonString2 = JSON.stringify(data2);
        info.data2 = jsonString2;

        var data3 = this.data.data3;
        const jsonString3 = JSON.stringify(data3);
        info.data3 = jsonString3;



        info.images1 = this.data.images1;
        info.images = this.data.images;
        info.images2 = this.data.images2;
        info.images3 = this.data.images3;

        info.allphoto = this.data.allphoto;
        info.allnum = this.data.allnum;
        info.allvideo = this.data.allvideo;
        info.mian = this.data.mian;
        info.minor = this.data.minor;

        info.case_id = this.data.id;

        console.log(info);


        api.post({
            url: 'live/submits',
            data: info,
            success: data => {
                var info = data.data;
                console.log(info)

                wx.showToast({
                    title: data.msg,
                    icon: 'none'
                })

                that.getinfobyid();

            }
        })


    },

    //   拍摄准备时间

    bindTimeChange1a(ret) {
        console.log(ret);
        this.setData({
            time1a: ret.detail.value
        })
    },
    bindTimeChange1b(ret) {
        console.log(ret);
        this.setData({
            time1b: ret.detail.value
        })
    },
    get1desc(ret) {
        this.setData({
            dest1: ret.detail.value
        })
    },
    adddata1() {

        var data1 = this.data.data1;
        var val = {};

        val['time1'] = this.data.time1a;
        val['time2'] = this.data.time1b;
        val['dest'] = this.data.dest1;

        var utime = this.timeStringToSeconds(this.data.time1a + ':00', this.data.time1b + ':00');

        if (!utime) {
            return false;
        }
        val['over'] = utime;

        data1.push(val);

        this.setData({
            data1: data1
        })


    },

    deleteme1(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;

        var data1 = this.data.data1;
        var n = [];
        data1.forEach(function (item, index) {

            if (index != i) {
                n.push(item);
            }

        });
        this.setData({
            data1: n
        })


    },

    deleteme2(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;

        var data1 = this.data.data2;
        var n = [];
        data1.forEach(function (item, index) {
            if (index != i) {
                n.push(item);
            }

        });
        this.setData({
            data2: n
        })


    },
    deleteme3(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;

        var data1 = this.data.data3;
        var n = [];
        data1.forEach(function (item, index) {
            if (index != i) {
                n.push(item);
            }
        });
        this.setData({
            data3: n
        })
    },
    deleteme4(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var data1 = this.data.data4;
        var n = [];
        data1.forEach(function (item, index) {
            if (index != i) {
                n.push(item);
            }
        });
        this.setData({
            data4: n
        })
    },

    //化妆时间

    bindTimeChange2a(ret) {
        console.log(ret);
        this.setData({
            time2a: ret.detail.value
        })
    },
    bindTimeChange2b(ret) {
        console.log(ret);
        this.setData({
            time2b: ret.detail.value
        })
    },
    get2desc(ret) {
        this.setData({
            dest2: ret.detail.value
        })
    },
    adddata2() {

        var data2 = this.data.data2;
        var val = {};

        val['time1'] = this.data.time2a;
        val['time2'] = this.data.time2b;
        val['dest'] = this.data.dest2;

        var utime = this.timeStringToSeconds(this.data.time2a + ':00', this.data.time2b + ':00');

        if (!utime) {
            return false;
        }
        val['over'] = utime;

        data2.push(val);

        this.setData({
            data2: data2
        })


    },

    //人物拍摄准备时间

    bindTimeChange3a(ret) {
        console.log(ret);
        this.setData({
            time3a: ret.detail.value
        })
    },
    bindTimeChange3b(ret) {
        console.log(ret);
        this.setData({
            time3b: ret.detail.value
        })
    },
    get3desc(ret) {
        this.setData({
            dest3: ret.detail.value
        })
    },
    adddata3() {

        var data3 = this.data.data3;
        var val = {};

        val['time1'] = this.data.time3a;
        val['time2'] = this.data.time3b;
        val['dest'] = this.data.dest3;

        var utime = this.timeStringToSeconds(this.data.time3a + ':00', this.data.time3b + ':00');

        if (!utime) {
            return false;
        }
        val['over'] = utime;

        data3.push(val);

        this.setData({
            data3: data3
        })


    },

    //产品拍摄准备时间

    bindTimeChange4a(ret) {
        console.log(ret);
        this.setData({
            time4a: ret.detail.value
        })
    },
    bindTimeChange4b(ret) {
        console.log(ret);
        this.setData({
            time4b: ret.detail.value
        })
    },
    get4desc(ret) {
        this.setData({
            dest4: ret.detail.value
        })
    },
    adddata4() {

        var data4 = this.data.data4;
        var val = {};

        val['time1'] = this.data.time4a;
        val['time2'] = this.data.time4b;
        val['dest'] = this.data.dest4;

        var utime = this.timeStringToSeconds(this.data.time4a + ':00', this.data.time4b + ':00');

        if (!utime) {
            return false;
        }
        val['over'] = utime;

        data4.push(val);

        this.setData({
            data4: data4
        })
    },

    //上传第8个图
    getimgs7() {
        let that = this;
        var usrl = api.HOST;
        var images7 = this.data.images7;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;
                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images7.push(img);

                            that.setData({
                                images7: images7
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })
                })

            }
        })
    },
    deleteimg7(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images7 = this.data.images7;
        var n = [];
        images7.forEach(function (item, index) {
            console.log(index, i);
            if (index != i) {
                n.push(item)
            }
        })

        this.setData({
            images7: n
        })
    },
    //上传第7个图
    getimgs6() {
        let that = this;
        var usrl = api.HOST;
        var images6 = this.data.images6;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;

                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images6.push(img);

                            that.setData({
                                images6: images6
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })

                })

            }
        })
    },
    deleteimg6(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images6 = this.data.images6;
        var n = [];


        images6.forEach(function (item, index) {

            console.log(index, i);
            if (index != i) {
                n.push(item)
            }
        })


        this.setData({
            images6: n
        })


    },

    //上传第6个图
    getimgs5() {
        let that = this;
        var usrl = api.HOST;
        var images = this.data.images5;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;

                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images.push(img);

                            that.setData({
                                images5: images
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })
                })

            }
        })
    },
    deleteimg5(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images5 = this.data.images5;
        var n = [];


        images5.forEach(function (item, index) {

            console.log(index, i);
            if (index != i) {
                n.push(item)
            }
        })


        this.setData({
            images5: n
        })


    },

    //上传第5个图
    getimgs4() {
        let that = this;
        var usrl = api.HOST;
        var images = this.data.images4;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;

                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images.push(img);

                            that.setData({
                                images4: images
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })
                })


            }
        })
    },
    deleteimg4(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images4 = this.data.images4;
        var n = [];


        images4.forEach(function (item, index) {

            console.log(index, i);
            if (index != i) {
                n.push(item)
            }
        })

        console.log(images4);
        this.setData({
            images4: n
        })


    },

    //上传第一个图
    getimgs() {
        let that = this;
        var usrl = api.HOST;
        var images = this.data.images;

        wx.chooseMedia({
            count: 9,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;
                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images.push(img);

                            that.setData({
                                images: images
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })
                })

            }
        })
    },
    deleteimg(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images = this.data.images;
        var n = [];
        images.forEach(function (item, index) {
            if (index != i) {
                n.push(item)
            }
        })
        this.setData({
            images: n
        })


    },

    getimgs1() {
        let that = this;
        var usrl = api.HOST;
        var images = this.data.images1;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;
                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images.push(img);

                            that.setData({
                                images1: images
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })

                })

            }
        })
    },
    deleteimg1(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images = this.data.images1;
        var n = [];
        images.forEach(function (item, index) {
            if (index != i) {
                n.push(item)
            }
        })
        this.setData({
            images1: n
        })


    },
    getimgs2() {
        let that = this;
        var usrl = api.HOST;
        var images = this.data.images2;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;

                tempFilePaths.forEach(function (item) {


                    wx.uploadFile({

                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images.push(img);

                            that.setData({
                                images2: images
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })

                })

            }
        })
    },
    deleteimg2(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images = this.data.images2;
        var n = [];
        images.forEach(function (item, index) {
            if (index != i) {
                n.push(item)
            }
        })
        this.setData({
            images2: n
        })


    },
    getimgs3() {
        let that = this;
        var usrl = api.HOST;
        var images = this.data.images3;

        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                console.log(res)

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFiles;

                tempFilePaths.forEach(function (item) {


                    wx.uploadFile({

                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data.data.img);
                            var img = data.data.img;

                            images.push(img);

                            that.setData({
                                images3: images
                            })
                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })
                })

            }
        })
    },
    deleteimg3(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var images = this.data.images3;
        var n = [];
        images.forEach(function (item, index) {
            if (index != i) {
                n.push(item)
            }
        })
        this.setData({
            images3: n
        })
    },


    getallphoto(ret) {
        this.setData({
            allphoto: ret.detail.value
        })
    },
    getallnum(ret) {
        this.setData({
            allnum: ret.detail.value
        })
    },
    getallvideo(ret) {
        this.setData({
            allvideo: ret.detail.value
        })
    },
    getmian(ret) {
        this.setData({
            mian: ret.detail.value
        })
    },
    getminor(ret) {
        this.setData({
            minor: ret.detail.value
        })
    },

    getallphoto1(ret) {
        this.setData({
            allphoto1: ret.detail.value
        })
    },
    getallnum1(ret) {
        this.setData({
            allnum1: ret.detail.value
        })
    },
    getallvideo1(ret) {
        this.setData({
            allvideo1: ret.detail.value
        })
    },
    getmian1(ret) {
        this.setData({
            mian1: ret.detail.value
        })
    },
    getminor1(ret) {
        this.setData({
            minor1: ret.detail.value
        })
    },


    // 时间计算
    timeStringToSeconds(timeStringa, timeStringb) {

        console.log(timeStringa, timeStringb)

        const [hours, minutes, seconds] = timeStringa.split(':').map(Number);
        const [hours1, minutes1, seconds1] = timeStringb.split(':').map(Number);

        var a = hours * 3600 + minutes * 60 + seconds;
        var b = hours1 * 3600 + minutes1 * 60 + seconds1;

        if ((b - a) < 1) {

            return false;
        }
        console.log(b, a)
        return this.secondsToTime(b - a);
    },
    secondsToTime(seconds) {
        // 计算小时数
        const hours = Math.floor(seconds / 3600);

        // 计算剩余的分钟数
        seconds %= 3600;
        const minutes = Math.floor(seconds / 60);

        // 计算剩余的秒数
        const secs = seconds % 60;

        // 格式化输出，确保小时、分钟和秒都是两位数
        const strHours = String(hours).padStart(2, '0');
        const strMinutes = String(minutes).padStart(2, '0');
        const strSeconds = String(secs).padStart(2, '0');
        var tip = '用时';
        tip += strHours > 0 ? strHours + '小时' : '';

        tip += strMinutes > 0 ? strMinutes + '分' : '';

        tip += strSeconds > 0 ? strSeconds + '秒' : '';
        // 返回时间字符串
        return tip;
    },

    customersign(ret) {
        console.log(ret);
        var i = ret.currentTarget.dataset.i;
        var id = this.data.id;
        var singimg = '';

        if (i == 1) {
            singimg = this.data.infos.sign1;
        }
        if (i == 2) {
            singimg = this.data.infos.sign2;
        }
        if (i == 3) {
            singimg = this.data.infos.sign3;
        }
        if (i == 4) {
            singimg = this.data.infos.sign4;
        }
        if (i == 5) {
            singimg = this.data.infos.sign5;
        }
        if (i == 6) {
            singimg = this.data.infos.sign6;
        }


        if (singimg) {
            wx.previewImage({
                current: singimg, // 当前显示图片的http链接
                urls: [singimg] // 需要预览的图片http链接列表
            });

        } else {

            wx.navigateTo({
                url: '/pages/live/sign?id=' + id + '&types=' + i
            })

        }





    },
    customersignb(ret){
        console.log(ret);
        var img=ret.currentTarget.dataset.img;
        wx.previewImage({
            current: img, // 当前显示图片的http链接
            urls: [img] // 需要预览的图片http链接列表
        });

    },

    goindex() {
        wx.navigateBack();
    },

    setshow() {
        this.setData({
            set: 1
        })
    },
    hidehow() {
        this.setData({
            set: 0
        })
    },

    set2show() {
        this.setData({
            set2: 1
        })
    },
    hide2show() {
        this.setData({
            set2: 0
        })
    },
    submitb2() {

        var info = {};
        var that = this;

        var data4 = this.data.data4;
        const jsonString4 = JSON.stringify(data4);
        info.data4 = jsonString4;

        info.images4 = this.data.images4;
        info.images5 = this.data.images5;
        info.images6 = this.data.images6;
        info.images7 = this.data.images7;



        info.allphoto1 = this.data.allphoto1;
        info.allnum1 = this.data.allnum1;
        info.allvideo1 = this.data.allvideo1;
        info.mian1 = this.data.mian1;
        info.minor1 = this.data.minor1;

        info.case_id = this.data.id;

        console.log(info);


        api.post({
            url: 'live/submits2',
            data: info,
            success: data => {
                var info = data.data;
                console.log(info)

                wx.showToast({
                    title: data.msg,
                    icon: 'none'
                })

            }
        })


    }




})